package com.ycshihuo.shihuo.backend.controller.sysUser;

import com.ycshihuo.shihuo.backend.service.sysUser.SysUserService;
import com.ycshihuo.shihuo.data.model.sysUser.User;
import com.ycshihuo.shihuo.util.PublicConstants;
import com.ycshihuo.shihuo.util.ReturnMessage;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by guoxingye on 2016/10/10.
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(User user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }
        return "登陆成功";
    }
    
    @RequestMapping("getSysUser")
    @ResponseBody
    public ReturnMessage getSysUser() {
        ReturnMessage msg = new ReturnMessage(PublicConstants.StatusCode.SUCCESS);
        try {
            msg = sysUserService.getSysUser();
        } catch (Exception e) {
            msg.setCode(PublicConstants.StatusCode.ERROR);
            msg.add(e.getMessage());
            return msg;
        }
        return msg;
    }
}
